package com.company.hrmadmin.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.hrmadmin.modules.domain.Role;
import com.company.hrmadmin.modules.domain.UserRole;
import com.company.hrmadmin.modules.service.RoleService;
import com.company.hrmadmin.modules.service.UserRoleService;
import com.company.hrmadmin.modules.mapper.UserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Xmirror
 * @description 针对表【sys_user_role】的数据库操作Service实现
 * @createDate 2022-03-18 15:39:37
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole>
        implements UserRoleService {

    @Resource
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleService roleService;

    @Override
    public UserRole selectUserRoleByUserId(Integer userId) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<UserRole>().eq("user_id", userId);
        List<UserRole> userRoles = userRoleMapper.selectList(queryWrapper);
        return userRoles == null || userRoles.size() == 0 ? null : userRoles.get(0);
    }

    @Override
    public Role selectByUserId(Integer userId) {
        UserRole userRole = this.selectUserRoleByUserId(userId);
        if (userRole == null) {
            return null;
        }
        return roleService.selectByRoleId(userRole.getRoleId());
    }

    @Override
    public String getRoleName(Integer userId) {
        Role role = this.selectByUserId(userId);
        return role == null ? null : role.getRole();
    }
}




